using UnityEngine;
using UnityEngine.EventSystems;

namespace g_pixelfarm
{
    public class UIButtonScale : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        [HideInInspector]
        public Vector3 StartScale = Vector3.one;
        public Vector3 EndScale = Vector3.one;
        public float Duration;

        private Vector3 fromValue = Vector3.zero;
        private Vector3 toValue = Vector3.zero;
        private bool running = false;
        private float elapsedTime = 0;

        void Start()
        {
            StartScale = transform.localScale;
        }

        public void Update()
        {
            if (running == false)
            {
                return;
            }

            elapsedTime += Time.deltaTime;

            var r = Mathf.Clamp01(elapsedTime / Duration);
            if (r == 1)
            {
                transform.localScale = toValue;
                running = false;
                return;
            }

            transform.localScale = Vector3.Lerp(fromValue, toValue, r);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            fromValue = transform.localScale;
            toValue = EndScale;
            elapsedTime = 0;
            running = true;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            fromValue = transform.localScale;
            toValue = StartScale;
            elapsedTime = 0;
            running = true;
        }
    }
}